<?php

class AdminController extends Controller
{
	public $layout = 'awt';
	
	public function actionIndex()
	{
		$this->render('index');
	}
	
	public function actionSearchSong() {
		
		$songSearcher = new SongSearcher();
		
		Yii::import('ext.zsearcher.Zsearcher');
		$searcher = new Zsearcher($songSearcher);
		$searcher->normalSearch();

		$this->render('song_search',array(
			'result'	=>$searcher->getResult(),
			'pager'		=>$searcher->getPager(),
			'filter'	=>$searcher->getFilter(),
		));

	}
	
	public function actionQuickviewSong($id) {
		
		$this->layout = 'awt-quickview';
		
		$rs = Song::model()->with('album')->findByPk($id);
		
		$this->render('song_quickview',array(
			'rs'	=>$rs
		));
	}
	
	public function actionCreateSong() {
		
		$model = new Song();
		
		if ( !empty($_POST) && isset($_POST['Song']) ) {
			
			$_POST['Song']['date_create'] = date('Y-m-d H:i:s');
			$_POST['Song']['user_create'] = Yii::app()->user->id;
			$model->attributes = $_POST['Song'];
			if ($model->save()) {
				
				Yii::app()->user->setFlash('success',"Data saved!");
			} else {
				echo "<pre>"; print_r( $model->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
			$this->refresh();
		}
		
		$this->render('song_create',array(
			'model'=>$model,
		));
	}
	
	public function actionUpdateSong($id) {
		
		$model = new Song('update');
		$model = $model->findByPk($id);

		if ( !empty($_POST) && isset($_POST['Song']) ) {
			
			$model->attributes = $_POST['Song'];
			if ($model->save()) {
				Yii::app()->user->setFlash('success',"Data saved!");
			} else {
				echo "<pre>"; print_r( $model->getErrors() ); exit;
			}
			$this->refresh();
		}
		$this->render('song_create',array(
			'model'=>$model,
		));
	}
	
	public function actionDeleteSong() {
		
		if ( !empty($_POST) && isset($_POST['Song']) ) {
			$rs = Song::model()->deleteByPk($_POST['Song']['id']);
			if ($rs) {
				Yii::app()->user->setFlash('success',"Song Deleted");
			} else {
				echo "<pre>"; print_r( $rs->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
		}
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	
	public function actionSearchAlbum() {
		
		$albumSearcher = new AlbumSearcher();
		
		Yii::import('ext.zsearcher.Zsearcher');
		$searcher = new Zsearcher($albumSearcher);
		$searcher->normalSearch();

		$this->render('album_search',array(
			'result'	=>$searcher->getResult(),
			'pager'		=>$searcher->getPager(),
			'filter'	=>$searcher->getFilter(),
		));

	}
	
	public function actionQuickviewAlbum($id) {
		
		$this->layout = 'awt-quickview';
		
		$rs = Album::model()->with('album')->findByPk($id);
		
		$this->render('album_quickview',array(
			'rs'	=>$rs
		));
	}
	
	public function actionCreateAlbum() {
		
		$model = new Album();
		
		if ( !empty($_POST) && isset($_POST['Album']) ) {
			
			$_POST['Album']['date_create'] = date('Y-m-d H:i:s');
			$_POST['Album']['user_create'] = Yii::app()->user->id;
			$model->attributes = $_POST['Album'];
			if ($model->save()) {
				
				Yii::app()->user->setFlash('success',"Data saved!");
			} else {
				echo "<pre>"; print_r( $model->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
			$this->refresh();
		}
		if (isset($_GET['aid'])) $model->artist_id = $_GET['aid'];
			
		$this->render('album_create',array(
			'model'=>$model,
		));
	}
	
	public function actionUpdateAlbum($id) {
		
		$model = new Album('update');
		$model = $model->findByPk($id);

		if ( !empty($_POST) && isset($_POST['Album']) ) {
			
			$model->attributes = $_POST['Album'];
			if ($model->save()) {
				Yii::app()->user->setFlash('success',"Data saved!");
			} else {
				echo "<pre>"; print_r( $model->getErrors() ); exit;
			}
			$this->refresh();
		}
		$this->render('album_create',array(
			'model'=>$model,
		));
	}
	
	public function actionDeleteAlbum() {
		
		if ( !empty($_POST) && isset($_POST['Album']) ) {
			$rs = Album::model()->deleteByPk($_POST['Album']['id']);
			if ($rs) {
				Yii::app()->user->setFlash('success',"Album Deleted");
			} else {
				echo "<pre>"; print_r( $rs->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
		}
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
	
	
	public function actionSearchArtist() {
		
		$artistSearcher = new ArtistSearcher();
		
		Yii::import('ext.zsearcher.Zsearcher');
		$searcher = new Zsearcher($artistSearcher);
		$searcher->normalSearch();

		$this->render('artist_search',array(
			'result'	=>$searcher->getResult(),
			'pager'		=>$searcher->getPager(),
			'filter'	=>$searcher->getFilter(),
		));

	}
	
	/*
	public function actionQuickviewArtist($id) {
		
		$this->layout = 'awt-quickview';
		
		$rs = Artist::model()->findByPk($id);
		
		$this->render('artist_quickview',array(
			'rs'	=> $rs
		));
	}*/
	
	public function actionCreateArtist() {
		
		$model = new Artist();
		
		if ( !empty($_POST) && isset($_POST['Artist']) ) {
			
			$_POST['Artist']['date_create'] = date('Y-m-d H:i:s');
			$_POST['Artist']['user_create'] = Yii::app()->user->id;
			$model->attributes = $_POST['Artist'];
			if ($model->save()) {
				
				Yii::app()->user->setFlash('success',"Data saved!");
			} else {
				echo "<pre>"; print_r( $model->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
			$this->refresh();
		}
		
		$this->render('artist_create',array(
			'model'=>$model,
		));
	}
	
	public function actionUpdateArtist($id) {
		
		$model = new Artist('update');
		$model = $model->findByPk($id);
		$this->pageTitle = '更新单位团体';

		if ( !empty($_POST) && isset($_POST['Artist']) ) {
			
			$model->attributes = $_POST['Artist'];
			if ($model->save()) {
				Yii::app()->user->setFlash('success',"Data saved!");
			} else {
				echo "<pre>"; print_r( $model->getErrors() ); exit;
			}
			$this->refresh();
		}
		$this->render('artist_create',array(
			'model'=>$model,
		));
	}
	public function actionSortArtist() {
		if (!empty($_POST)) {
			foreach ($_POST as $id => $pos) {
				Yii::app()->db->createCommand()
					->update('artist',array(
						'pos'=>$pos,
					),'id=:id',array(':id'=>$id));
				
			}
		}
		Yii::app()->end();
	}
	public function actionDeleteArtist() {
		
		if ( !empty($_POST) && isset($_POST['Artist']) ) {
			$rs = Artist::model()->deleteByPk($_POST['Artist']['id']);
			if ($rs) {
				Yii::app()->user->setFlash('success',"Artist Deleted");
			} else {
				echo "<pre>"; print_r( $rs->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
		}
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
}